#include "ProgressEx.h"

CProgressEx::CProgressEx():CProgress()
{
}

CProgressEx::~CProgressEx()
{
}

CProgressEx::CProgressEx(UINT nID):CProgress(nID)
{
}

CProgressEx::CProgressEx(wstring strCaption,UINT nID):CProgress(strCaption,nID)
{
}

CProgressEx::CProgressEx(DWORD dwStyle,wstring strCaption, const CRect &rc, UINT nID):CProgress(dwStyle,strCaption,rc,nID)
{
}

CProgressEx::CProgressEx(DWORD dwStyle, wstring strCaption, int x, int y, int cx, int cy, UINT nID):CProgress(dwStyle,strCaption,x,y,cx,cy,nID)
{
}

void CProgressEx::Paint(CDC* pDC)
{
	if(m_rcControl.IsRectEmpty())
	{
		return ;
	}

	if(!IsVisible())
	{
		return;
	}

	PaintBackGnd(pDC);

	CDC dcMem;
	dcMem.CreateCompatibleDC(pDC->m_hDC);

	HBITMAP pbmTmp;
	// Draw channel
	if( m_bChannel ) 
	{
		pbmTmp = dcMem.SelectBitmap( m_bmChannel.m_hBitmap );
		// There is a bitmap for active channel
		if( m_bChannelActive) 
		{
			// Vertical slider
			if( m_bVertical ) 
			{
				// Lower part
				DrawBitmap( pDC, m_rcControl.left, m_rcControl.top,
					m_nWidth, m_nHeight - Pos2Pixel(m_nPos),
					&dcMem, 0, 0
					);

				dcMem.SelectBitmap(m_bmChannelActive.m_hBitmap);
				// Upper part
				DrawBitmap(pDC,m_rcControl.left,m_rcControl.bottom-Pos2Pixel(m_nPos),m_nWidth,Pos2Pixel(m_nPos),&dcMem, 0,m_rcControl.Height()-Pos2Pixel(m_nPos));
			}
			else // Horizontal slider
			{
				// Right side
				DrawBitmap(pDC, m_rcControl.left+Pos2Pixel(m_nPos),m_rcControl.top,
					m_nWidth - Pos2Pixel(m_nPos), m_nHeight,
					&dcMem, Pos2Pixel(m_nPos), 0
					);

				dcMem.SelectBitmap( m_bmChannelActive.m_hBitmap );
				// Left side
				DrawBitmap(pDC, m_rcControl.left, m_rcControl.top, Pos2Pixel(m_nPos), m_nHeight,&dcMem, 0, 0);
			}

			// Only one bitmap for channel
		}
		else
		{
			DrawBitmap(pDC, m_rcControl.left, m_rcControl.top, m_nWidth, m_nHeight,	&dcMem, 0, 0);
		}

		dcMem.SelectBitmap( pbmTmp );
	}

	// Draw thumb
	if( m_bThumb)
	{
		if( m_bThumbActive && (m_nState & STATE_PUSHED))
			pbmTmp = dcMem.SelectBitmap(m_bmThumbActive.m_hBitmap); // Active thumb
		else
			pbmTmp = dcMem.SelectBitmap(m_bmThumb.m_hBitmap); // Normal thumb

		// Vertical slider
		if( m_bVertical )
		{
			DrawBitmap(
				pDC, m_rcControl.left+m_nMarginLeft, m_rcControl.bottom-Pos2Pixel(m_nPos) - m_nThumbHeight,
				m_nThumbWidth, m_nThumbHeight,&dcMem, 0, 0);
		} 
		else // Horizontal slider
		{
			DrawBitmap(
				pDC, m_rcControl.left+Pos2Pixel(m_nPos) - m_nThumbWidth, m_rcControl.top+m_nMarginTop,
				m_nThumbWidth, m_nThumbHeight,&dcMem, 0, 0);

		} // if horizontal

		dcMem.SelectBitmap( pbmTmp );

	} // if draw thumb

	dcMem.DeleteDC();
}

void CProgressEx::Click(DWORD dwClick, POINT point)
{
	BOOL bSendMsg = FALSE;
	if(!IsEnabled())
	{
		return;
	}

	if(!m_bVisibale)
	{
		return;
	}

	if(!m_pContainer || !m_pContainer->m_hWnd)
	{
		return;
	}

	if(!m_rcControl.PtInRect(point))
	{ 
		m_nState = STATE_NORMAL;
		return;
	}
	else
	{
		if(dwClick & CLICK_DOWN)
		{			
			m_nState = STATE_PUSHED;
			bSendMsg = TRUE;
		}
		else if(dwClick & CLICK_UP)
		{
			if(m_pContainer->m_hWnd)
				//PostMessage(m_pContainer->m_hWnd,WM_COMMAND, m_nID,NULL);
				bSendMsg = TRUE;
			m_nState = STATE_NORMAL;
		}
	}

	point.x -= m_rcControl.left;
	point.y -= m_rcControl.top;

	/////////////////////////////////////////////////////

	if( m_bVertical ) 
	{
		if( abs(m_nHeight-point.y - Pos2Pixel( m_nPos ) ) <= m_nThumbHeight / 2 )
			m_nMouseOffset = m_nHeight - point.y - Pos2Pixel( m_nPos );
		else
			m_nMouseOffset = 0;
	}
	else
	{
		if( abs( point.x - Pos2Pixel( m_nPos ) ) <= m_nThumbWidth / 2 )
			m_nMouseOffset = point.x - Pos2Pixel( m_nPos );
		else
			m_nMouseOffset = 0;
	}

	/////////////////////////////////////

	int nPixel;

	// Boundary check
	if( m_bVertical )
	{
		nPixel = m_nHeight - point.y - m_nMouseOffset;

		if( nPixel > m_nHeight - m_nMarginBottom - m_nThumbHeight/2 )
			nPixel = m_nHeight - m_nMarginBottom - m_nThumbHeight/2;

		if( nPixel < m_nMarginTop + m_nThumbHeight/2 )
			nPixel = m_nMarginTop + m_nThumbHeight/2;

	} 
	else
	{
		nPixel = point.x - m_nMouseOffset;

		if( nPixel < m_nMarginLeft + m_nThumbWidth/2 )
			nPixel = m_nMarginLeft + m_nThumbWidth/2;

		if( nPixel > m_nWidth - m_nMarginRight - m_nThumbWidth/2 )
			nPixel = m_nWidth - m_nMarginRight - m_nThumbWidth/2;
	}

	// Apply change
	if( Pos2Pixel(m_nPos) != nPixel )
	{
		SetPos( Pixel2Pos( nPixel ) );
	}

	if(bSendMsg)
	{
		PostMessage(m_pContainer->m_hWnd,WM_COMMAND, m_nID,NULL);
	}
}